﻿''' <summary>GUI for adding and deleteing dictionaries</summary>
Public Class frmDicManagement
    ''' <summary>key of default image</summary>
    Private Const DefaultImage As String = "default"

    ''' <summary>Loads dictionaries into list view</summary>
    Private Sub LoadDictionaries()
        Dim DefLarge As Image = imlIconsLarge.Images!default
        Dim DefSmall As Image = imlIconsSmall.Images!default
        imlIconsLarge.Images.Clear()
        imlIconsSmall.Images.Clear()
        imlIconsLarge.Images.Add(DefaultImage, DefLarge)
        imlIconsSmall.Images.Add(DefaultImage, DefSmall)
        Dim ta As New dsDicManagementTableAdapters.taDictionary
        For Each row As dsDicManagement.DictionaryRow In ta.GetData
            Dim item As ListViewItem = lvwDics.Items.Add(row.NameE)
            item.SubItems.Add(row.Name1)
            item.SubItems.Add(row.Name2)
            item.SubItems.Add(row.English1)
            item.SubItems.Add(row.Native1)
            item.SubItems.Add(row.Other1)
            item.SubItems.Add(row.Lang1Code)
            item.SubItems.Add(row.English2)
            item.SubItems.Add(row.Other2)
            item.SubItems.Add(row.Native2)
            item.SubItems.Add(row.Lang2Code)
            item.SubItems.Add(row.Version)
            item.Tag = row
            If row.ICO <> "" AndAlso (IconFolder + row.ICO).IsFile Then
                Try
                    imlIconsLarge.Images.Add(row.NameE, New Icon(IconFolder + row.ICO))
                    imlIconsSmall.Images.Add(row.NameE, New Icon(IconFolder + row.ICO))
                    item.ImageKey = row.NameE
                Catch
                    item.ImageKey = DefaultImage
                End Try
            Else
                item.ImageKey = DefaultImage
            End If
        Next row
    End Sub
    ''' <summary>CTor</summary>
    Public Sub New()
        InitializeComponent()
        LoadDictionaries()
        lvwDics_SelectedIndexChanged(lvwDics, EventArgs.Empty)
    End Sub
    ''' <summary>Shows details of currently selected dictionary</summary>
    ''' <param name="Dictionary">Dictionary to show details of</param>
    Private Sub ShowDetails(ByVal Dictionary As dsDicManagement.DictionaryRow)
        picDicIco.Image = Nothing
        picIco1.Image = Nothing
        picIco2.Image = Nothing
        If Dictionary IsNot Nothing Then
            With Dictionary
                'Icons
                If .ICO <> "" AndAlso (IconFolder + .ICO).IsFile Then
                    Try
                        picDicIco.Image = New Icon(IconFolder + .ICO).ToBitmap
                    Catch : End Try
                End If
                If (IconFolder + (.Lang1Code & ".ico")).IsFile Then
                    Try
                        picIco1.Image = New Icon(IconFolder + (.Lang1Code & ".ico")).ToBitmap
                    Catch : End Try
                End If
                If (IconFolder + (.Lang2Code & ".ico")).IsFile Then
                    Try
                        picIco2.Image = New Icon(IconFolder + (.Lang2Code & ".ico")).ToBitmap
                    Catch : End Try
                End If
                txtVersion.Text = .Version
                txtNameE.Text = .NameE
                txtName1.Text = .Name1
                txtName2.Text = .Native2
                txtEnglish1.Text = .English1
                txtNative1.Text = .Native1
                txtNative2.Text = .Native2
                txtEnglish2.Text = .English2
                txtOther1.Text = .Other1
                txtOther2.Text = .Other2
                txtLang1Code.Text = .Lang1Code
                txtLang2Code.Text = .Lang2Code
                txtICO.Text = .ICO
                txtPriICO1.Text = .PriICO1
                txtPriICO2.Text = .PriICO2
                chkPrimary.CheckState = CheckState.Checked
                chkPrimary.Checked = .Primary
                'Author
                txtAuthNick.Text = .AuthNick
                txtAuthFirstN.Text = .AuthFirstN
                txtAuthLastN.Text = .AuthLastN
                txtAuthEMail.Text = .AuthEMail
                txtAuthWWW.Text = .AuthWWW
                txtAuthNote.Text = .AuthNote
                'Original author
                txtOrigNick.Text = .OrigNick
                txtOrigFirstN.Text = .OrigFirstN
                txtOrigLastN.Text = .OrigLastN
                txtOrigEMail.Text = .OrigEMail
                txtOrigWWW.Text = .OrigWWW
                txtOrigNote.Text = .OrigNote
                'Statistic
                lblIn1CntI.Text = String.Format(My.Resources.WordsIn, .Lang1Code)
                lblIn2CntI.Text = String.Format(My.Resources.WordsIn, .Lang2Code)
                Dim Row As dsDicManagement.WordsCountRow = New dsDicManagementTableAdapters.taWordsCount().GetDataByDic(.ID)(0)
                lblIn1Cnt.Text = Row.Words1
                lblIn2Cnt.Text = Row.Words2
            End With
            fraDetails.Enabled = True
        Else
            For Each txt As Control In New Control() {txtVersion, txtNameE, txtName1, txtName2, txtEnglish1, txtNative1, txtNative2, txtOther1, txtOther2, txtLang1Code, txtLang2Code, txtICO, txtPriICO1, txtPriICO2, txtAuthFirstN, txtAuthLastN, txtAuthEMail, txtAuthWWW, txtAuthNote, txtOrigNick, txtOrigFirstN, txtOrigLastN, txtOrigEMail, txtOrigWWW, txtOrigNote, txtAuthNick, lblIn1CntI, lblIn1Cnt, lblIn2CntI, lblIn2Cnt, txtEnglish2}
                txt.Text = ""
            Next txt
            chkPrimary.CheckState = CheckState.Indeterminate
            fraDetails.Enabled = False
        End If
    End Sub

    Private Sub lvwDics_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwDics.SelectedIndexChanged
        EditMode = False
        If lvwDics.SelectedItems.Count > 0 Then
            ShowDetails(lvwDics.SelectedItems(0).Tag)
        Else
            ShowDetails(Nothing)
        End If
    End Sub
    ''' <summary>Changes edit/read-only mode of controls</summary>
    ''' <value>New stage of controls</value>
    Private WriteOnly Property EditMode() As Boolean
        Set(ByVal value As Boolean)
            For Each txt As TextBox In New TextBox() {txtVersion, txtNameE, txtName1, txtName2, txtEnglish1, txtNative1, txtNative2, txtOther1, txtOther2, txtLang1Code, txtLang2Code, txtICO, txtPriICO1, txtPriICO2, txtAuthFirstN, txtAuthLastN, txtAuthEMail, txtAuthWWW, txtAuthNote, txtOrigNick, txtOrigFirstN, txtOrigLastN, txtOrigEMail, txtOrigWWW, txtOrigNote, txtAuthNick, txtEnglish2}
                txt.ReadOnly = Not value
            Next txt
            chkPrimary.AutoCheck = value
            For Each txt As TextBox In New TextBox() {txtAuthEMail, txtAuthWWW, txtOrigEMail, txtOrigWWW}
                If Not value AndAlso txt.Text <> "" Then
                    txt.Font = New Font(txt.Font, FontStyle.Underline)
                    txt.Cursor = Cursors.Hand
                    txt.ForeColor = Tools.DrawingT.SystemColorsExtension.BrowserLink
                Else
                    txt.Font = New Font(txt.Font, FontStyle.Regular)
                    txt.Cursor = Cursors.IBeam
                    txt.ForeColor = SystemColors.WindowText
                End If
            Next txt
            cmdEdit.Enabled = Not value
        End Set
    End Property

    Private Sub txtAuthEMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles txtOrigWWW.Click, txtOrigEMail.Click, txtAuthWWW.Click, txtAuthEMail.Click
        With DirectCast(sender, TextBox)
            If .Cursor = Cursors.Hand AndAlso .Text <> "" Then
                Dim prefix As String = ""
                If sender Is txtAuthEMail OrElse sender Is txtOrigEMail Then prefix = "mailto:"
                Try
                    Process.Start(prefix & .Text)
                Catch : End Try
            End If
        End With
    End Sub

    Private Sub txtAuthEMail_ReadOnlyChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOrigWWW.ReadOnlyChanged, txtOrigEMail.ReadOnlyChanged, txtAuthWWW.ReadOnlyChanged, txtAuthEMail.ReadOnlyChanged
        If DirectCast(sender, TextBox).ReadOnly Then
            DirectCast(sender, TextBox).BackColor = SystemColors.Control
        Else
            DirectCast(sender, TextBox).BackColor = SystemColors.Window
        End If
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        fraDetails.Select()
        EditMode = True
    End Sub

    Private Sub chkPrimary_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPrimary.CheckedChanged
        lblPriICO1.Enabled = chkPrimary.Checked
        lblPriICO2.Enabled = chkPrimary.Checked
        txtPriICO1.Enabled = chkPrimary.Checked
        txtPriICO2.Enabled = chkPrimary.Checked
        If chkPrimary.Checked = True AndAlso cmdEdit.Enabled = False Then
            txtPriICO1.Text = txtLang1Code.Text & ".ico"
            txtPriICO2.Text = txtLang2Code.Text & ".ico"
        End If
    End Sub

    Private Function SaveChanges() As Boolean
        Dim ta As New dsDicManagementTableAdapters.taDictionary
        Dim row As dsDicManagement.DictionaryRow = lvwDics.SelectedItems(0).Tag
        Try 'TODO:Resources
            'TODO:Dictionary note seems to be completely ignored
            'Author
            If txtAuthEMail.Text <> "" AndAlso Not MailMatch.IsMatch(txtAuthEMail.Text) Then
                MsgBox("Specified author's @-mail is invalid.", MsgBoxStyle.Exclamation, My.Resources.Error_)
                txtAuthEMail.Select()
                Return False
            ElseIf txtAuthEMail.Text = "" Then
                MsgBox("Author's @-mail must be filled in.", MsgBoxStyle.Exclamation, My.Resources.Error_)
                txtAuthEMail.Select()
                Return False
            End If
            row.AuthEMail = txtAuthEMail.Text
            If txtAuthFirstN.Text = "" Then
                MsgBox("Author's first name must be filled in", MsgBoxStyle.Exclamation, My.Resources.Error_)
                txtAuthFirstN.Select()
                Return False
            End If
            row.AuthFirstN = txtAuthFirstN.Text
            If txtAuthLastN.Text = "" Then
                MsgBox("Author's first name must be filled in", MsgBoxStyle.Exclamation, My.Resources.Error_)
                txtAuthLastN.Select()
                Return False
            End If
            row.AuthLastN = txtAuthLastN.Text
            If txtAuthNick.Text = "" Then
                MsgBox(My.Resources.AuthorsNickMustBeFilledIn, MsgBoxStyle.Exclamation, My.Resources.Error_)
                txtAuthNick.Select()
                Return False
            End If
            row.AuthNick = txtAuthNick.Text
            row.AuthNote = txtAuthNote.Text
            If txtAuthWWW.Text <> "" AndAlso Not WWWMatch.IsMatch(txtAuthWWW.Text) Then
                MsgBox("Specified author's web page is invalid. Ensure that http:// is specified", MsgBoxStyle.Exclamation, My.Resources.Error_)
                txtAuthWWW.Select()
                Return False
            End If
            row.AuthWWW = txtAuthWWW.Text
            'DIctionary info
            'TODO:Verify that compulsory fields are filled in
            For Each Compulsory As KeyValuePair(Of String, TextBox) In New KeyValuePair(Of String, TextBox)() { _
                    New KeyValuePair(Of String, TextBox)("Name of language 1 in English", txtEnglish1), _
                    New KeyValuePair(Of String, TextBox)("Name of language 2 in English", txtEnglish2), _
                    New KeyValuePair(Of String, TextBox)("Code of laguage 1", txtLang1Code), _
                    New KeyValuePair(Of String, TextBox)("Code of laguage 2", txtLang2Code), _
                    New KeyValuePair(Of String, TextBox)("Name of dictionary in language 1", txtName1), _
                    New KeyValuePair(Of String, TextBox)("Name of dictionary in language 2", txtName2), _
                    New KeyValuePair(Of String, TextBox)("Name of dictionary in English", txtNameE), _
                    New KeyValuePair(Of String, TextBox)("Name of language 1 in itself", txtNative1), _
                    New KeyValuePair(Of String, TextBox)("Name of language 2 in itself", txtNative2), _
                    New KeyValuePair(Of String, TextBox)("Name of language 1 in language 2", txtOther1), _
                    New KeyValuePair(Of String, TextBox)("Name of language 2 in language 1", txtOther2), _
                    New KeyValuePair(Of String, TextBox)("Version", txtVersion) _
            }
                If Compulsory.Value.Text = "" Then
                    MsgBox(String.Format("{0} must be filled in", Compulsory.Key), MsgBoxStyle.Exclamation, My.Resources.Error_)
                    Compulsory.Value.Select()
                    Return False
                End If
            Next Compulsory

            'TODO:Date field is missing
            row.English1 = txtEnglish1.Text
            row.English2 = txtEnglish2.Text
            If txtICO.Text <> "" AndAlso Not (IconFolder + txtICO.Text).IsFile Then
                MsgBox("Icon file specified doesn't exist", MsgBoxStyle.Exclamation, My.Resources.Error_)
                txtICO.Select()
                Return False
            End If
            row.ICO = txtICO.Text
            row.Lang1Code = txtLang1Code.Text
            row.Lang2Code = txtLang2Code.Text
            row.Name1 = txtName1.Text
            row.Name2 = txtName2.Text
            row.NameE = txtNameE.Text
            row.Native1 = txtNative1.Text
            row.Native2 = txtNative2.Text
            row.Other1 = txtOther1.Text
            row.Other2 = txtOther2.Text
            If Not VersionMatch.IsMatch(txtVersion.Text) Then
                MsgBox("Specified version number is invalid. Version must consist of from 2 to 4 groups of numbers. Group of number cannot start with 0 unless it contains only 0. Valid version numbers are 1.3, 1.8.9, 0.0.0.0 etc.", MsgBoxStyle.Exclamation, My.Resources.Error_)
                txtVersion.Select()
                Return False
            End If
            row.Version = txtVersion.Text 'TODO:Verify against pattern
            'Original author
            If txtOrigEMail.Text <> "" OrElse txtOrigFirstN.Text <> "" OrElse txtOrigLastN.Text <> "" OrElse txtOrigNick.Text <> "" OrElse txtOrigNote.Text <> "" OrElse txtOrigWWW.Text <> "" Then
                If txtOrigEMail.Text <> "" AndAlso Not MailMatch.IsMatch(txtOrigEMail.Text) Then
                    MsgBox("Specified original author's @-mail is invalid.", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtOrigEMail.Select()
                    Return False
                ElseIf txtOrigEMail.Text = "" Then
                    MsgBox("Original author's @-mail must be filled in.", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtOrigEMail.Select()
                    Return False
                End If
                row.OrigEMail = txtOrigEMail.Text
                If txtOrigFirstN.Text = "" Then
                    MsgBox("Original author's first name must be filled in", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtOrigFirstN.Select()
                    Return False
                End If
                row.OrigFirstN = txtOrigFirstN.Text
                If txtOrigLastN.Text = "" Then
                    MsgBox("Original authors's first name must be filled in", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtOrigLastN.Select()
                    Return False
                End If
                row.OrigLastN = txtOrigLastN.Text
                If txtOrigNick.Text = "" Then
                    MsgBox("Original author's nick must be filled in.", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtOrigNick.Select()
                    Return False
                End If
                row.OrigNick = txtOrigNick.Text
                row.OrigNote = txtOrigNote.Text
                If txtOrigWWW.Text <> "" AndAlso Not WWWMatch.IsMatch(txtOrigWWW.Text) Then
                    MsgBox("Specified oroginal authors's web page is invalid. Ensure that http:// is specified", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtOrigWWW.Select()
                    Return False
                End If
                row.OrigWWW = txtOrigWWW.Text
            End If
            'Additional info for primary dictionary
            row.Primary = chkPrimary.Checked
            If chkPrimary.Checked Then
                If txtPriICO1.Text.ToLower <> (txtLang1Code.Text & ".ico").ToLower Then
                    MsgBox("File name for language icon must be same as language code and .ico extension.", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtPriICO1.Select()
                    Return False
                ElseIf Not (IconFolder + txtPriICO1.Text).IsFile Then
                    MsgBox("Icon file specified doesn't exist", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtPriICO1.Select()
                    Return False
                End If
                row.PriICO1 = txtPriICO1.Text
                If txtPriICO2.Text.ToLower <> (txtLang2Code.Text & ".ico").ToLower Then
                    MsgBox("File name for language icon must be same as language code and .ico extension.", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtPriICO2.Select()
                    Return False
                ElseIf Not (IconFolder + txtPriICO2.Text).IsFile Then
                    MsgBox("Icon file specified doesn't exist", MsgBoxStyle.Exclamation, My.Resources.Error_)
                    txtPriICO2.Select()
                    Return False
                End If
                row.PriICO2 = txtPriICO2.Text
            Else
                row.PriICO1 = ""
                row.PriICO2 = ""
            End If
            Try
                ta.Update(row)
            Catch ex As Exception
                MsgBox(My.Resources.ErrorWhileUpdatingDictionaryInfo & vbCrLf & ex.Message, MsgBoxStyle.Critical, My.Resources.Error_)
                Return False
            End Try
            Return True
        Finally
            lvwDics.SelectedItems(0).Tag = ta.GetDataByID(row.ID)(0)
            UpdateRow(lvwDics.SelectedItems(0))
        End Try
    End Function
    ''' <summary>Updates view of row</summary>
    ''' <param name="Row">Row to be updated</param>
    Private Sub UpdateRow(ByVal Row As ListViewItem)
        Dim rData As dsDicManagement.DictionaryRow = Row.Tag
        Row.SubItems(cohEnglish1.Index).Text = rData.English1
        Row.SubItems(cohEnglish2.Index).Text = rData.English2
        Row.SubItems(cohLang1Code.Index).Text = rData.Lang1Code
        Row.SubItems(cohEnglish2.Index).Text = rData.Lang2Code
        Row.SubItems(cohName1.Index).Text = rData.Name1
        Row.SubItems(cohName2.Index).Text = rData.Name2
        Row.SubItems(cohNative1.Index).Text = rData.Native1
        Row.SubItems(cohNative2.Index).Text = rData.Native2
        Row.SubItems(cohNameE.Index).Text = rData.NameE
        Row.SubItems(cohOther1.Index).Text = rData.Other1
        Row.SubItems(cohOther2.Index).Text = rData.Other2
        Row.SubItems(cohVersion.Index).Text = rData.Version
    End Sub

    Private Sub fraDetails_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles fraDetails.Validating
        If cmdEdit.Enabled = False Then
            If SaveChanges() Then
                EditMode = False
            Else
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub txtLang1Code_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLang1Code.TextChanged
        If cmdEdit.Enabled = False AndAlso chkPrimary.Checked = True Then
            txtPriICO1.Text = txtLang1Code.Text & ".ico"
        End If
    End Sub

    Private Sub txtLang2Code_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLang2Code.TextChanged
        If cmdEdit.Enabled = False AndAlso chkPrimary.Checked = True Then
            txtPriICO2.Text = txtLang2Code.Text & ".ico"
        End If
    End Sub
End Class